using KenEgozi.Com.Domain.Aggregates.Post;
using KenEgozi.Com.Domain.Interfaces.Repositories;
using KenEgozi.Com.Weblog.Controllers;
using KenEgozi.Com.Weblog.Services;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using Rhino.Commons;
using System.Collections;

namespace KenEgozi.Com.Weblog.Tests
{
	[TestFixture]
	public class CommentsControllerFixture : BaseControllerTest<CommentsController>
	{
		#region setup stuff
		private IPostsRepository postsRepository;
		private IRepository<Comment> commentsRepository;
		private ICachedItemsService cache;

		[SetUp]
		public override void SetUp()
		{
			// boring
			base.SetUp();
			// boring (@Ayende: no offense)
			postsRepository = mocks.DynamicMock<IPostsRepository>();
			commentsRepository = mocks.DynamicMock<IRepository<Comment>>();
			cache = mocks.DynamicMock<ICachedItemsService>();

			// interesting ...
			controller = new CommentsController(commentsRepository, cache);
			PrepareController(controller, "", "Comments", "");
		}
		#endregion

		#region ListByPost(int postId)
		/*Demo*/
		[Test]
		public void ListByPost_Always_SetPropertyBagParameter()
		{
			Comment comment = new Comment(
				"Please buy viagra from me",
				"The Man",
				"the@email.com",
				"http:/the.home.com",
				"1.2.3.4");

			Post post = new Post();
			post.Comments.Add(comment);

			Expect.Call(postsRepository.Get(0))
				.IgnoreArguments()
				.Return(post);
			LastCall.Message("postsRepository.Get() was not called");

			mocks.ReplayAll();

			controller.ListByPost(0, false);

			mocks.VerifyAll();

			Assert.That(
				controller.PropertyBag["comments"], 
				Is.EquivalentTo((ICollection)post.Comments),
				"comments parameter was not set properly");
		}
		/**/
		#endregion
		#region Delete(int[] ids)
		/*Demo*/
		[Test]
		public void Delete_Always_CallsRepositoryDelete()
		{
			Comment comment1 = new Comment(
				"Please buy viagra from me",
				"The Man",
				"the@email.com",
				"http:/the.home.com",
				"1.2.3.4");
			Comment comment2 = new Comment(
				"Please buy viagra from me",
				"The Man",
				"the@email.com",
				"http:/the.home.com",
				"1.2.3.4");

			Expect.Call(commentsRepository.Get(1))
				.Return(comment1);
			commentsRepository.Delete(comment1);
			Expect.Call(commentsRepository.Get(2))
				.Return(comment2);
			commentsRepository.Delete(comment2);

			mocks.ReplayAll();

			controller.Delete(new int[] { 1, 2 });

			mocks.VerifyAll();
		}	
		/**/
		#endregion
	}
}
